/*
 * Copyright [2011] [Ambud Sharma/ODep Monitor]

   Licensed under the Apache License, Version 2.0 (the "License");

   you may not use this file except in compliance with the License.

   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software

   distributed under the License is distributed on an "AS IS" BASIS,

   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

   See the License for the specific language governing permissions and

   limitations under the License.
 */
package edu.uwsp.orion.odepm.main.client;

import java.util.ArrayList;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.DoubleClickEvent;
import com.google.gwt.event.dom.client.DoubleClickHandler;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.shared.EventHandler;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;

import edu.uwsp.orion.odepm.main.shared.Node;
import edu.uwsp.orion.odepm.main.shared.NodeDetails;
import edu.uwsp.orion.odepm.main.shared.Rack;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Image;

public class RackView extends Composite {

	public RackView(Rack rack) {
		
		ArrayList<Image> allnode=new ArrayList<Image>(20);
		
		AbsolutePanel apContainerRackView = new AbsolutePanel();
		apContainerRackView.setStyleName("JobConf");
		initWidget(apContainerRackView);
		apContainerRackView.setSize("159px", "221px");
		
		Label lblNodeName = new Label(rack.rackName);
		lblNodeName.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		lblNodeName.setStyleName("gwt-Label-JobLauncherHeader");
		apContainerRackView.add(lblNodeName, 10, 0);
		lblNodeName.setSize("133px", "22px");
		
		Image image = new Image("images/NodeLive.png");
		apContainerRackView.add(image, 10, 21);
		image.setSize("30px", "34px");
		allnode.add(image);
		
		Image image_1 = new Image("images/NodeLive.png");
		apContainerRackView.add(image_1, 46, 21);
		image_1.setSize("30px", "34px");
		allnode.add(image_1);
		
		Image image_2 = new Image("images/NodeLive.png");
		apContainerRackView.add(image_2, 81, 21);
		image_2.setSize("30px", "34px");
		allnode.add(image_2);
		
		Image image_3 = new Image("images/NodeLive.png");
		apContainerRackView.add(image_3, 117, 21);
		image_3.setSize("30px", "34px");
		allnode.add(image_3);
		
		Image image_4 = new Image("images/NodeLive.png");
		apContainerRackView.add(image_4, 10, 61);
		image_4.setSize("30px", "34px");
		allnode.add(image_4);
		
		Image image_5 = new Image("images/NodeLive.png");
		apContainerRackView.add(image_5, 46, 61);
		image_5.setSize("30px", "34px");
		allnode.add(image_5);
		
		Image image_6 = new Image("images/NodeLive.png");
		apContainerRackView.add(image_6, 81, 61);
		image_6.setSize("30px", "34px");
		allnode.add(image_6);
		
		Image image_7 = new Image("images/NodeLive.png");
		apContainerRackView.add(image_7, 117, 61);
		image_7.setSize("30px", "34px");
		allnode.add(image_7);
		
		Image image_8 = new Image("images/NodeLive.png");
		apContainerRackView.add(image_8, 10, 101);
		image_8.setSize("30px", "34px");
		allnode.add(image_8);
		
		Image image_9 = new Image("images/NodeLive.png");
		apContainerRackView.add(image_9, 46, 101);
		image_9.setSize("30px", "34px");
		allnode.add(image_9);
		
		Image image_10 = new Image("images/NodeLive.png");
		apContainerRackView.add(image_10, 81, 101);
		image_10.setSize("30px", "34px");
		allnode.add(image_10);
		
		Image image_11 = new Image("images/NodeLive.png");
		apContainerRackView.add(image_11, 117, 101);
		image_11.setSize("30px", "34px");
		allnode.add(image_11);
		
		Image image_12 = new Image("images/NodeLive.png");
		apContainerRackView.add(image_12, 10, 141);
		image_12.setSize("30px", "34px");
		allnode.add(image_12);
		
		Image image_13 = new Image("images/NodeLive.png");
		apContainerRackView.add(image_13, 46, 141);
		image_13.setSize("30px", "34px");
		allnode.add(image_13);
		
		Image image_14 = new Image("images/NodeLive.png");
		apContainerRackView.add(image_14, 81, 141);
		image_14.setSize("30px", "34px");
		allnode.add(image_14);
		
		Image image_15 = new Image("images/NodeLive.png");
		apContainerRackView.add(image_15, 117, 141);
		image_15.setSize("30px", "34px");
		allnode.add(image_15);
		
		Image image_16 = new Image("images/NodeLive.png");
		apContainerRackView.add(image_16, 10, 181);
		image_16.setSize("30px", "34px");
		allnode.add(image_16);
		
		Image image_17 = new Image("images/NodeLive.png");
		apContainerRackView.add(image_17, 46, 181);
		image_17.setSize("30px", "34px");
		allnode.add(image_17);
		
		Image image_18 = new Image("images/NodeLive.png");
		apContainerRackView.add(image_18, 81, 181);
		image_18.setSize("30px", "34px");
		allnode.add(image_18);
		
		Image image_19 = new Image("images/NodeLive.png");
		apContainerRackView.add(image_19, 117, 181);
		image_19.setSize("30px", "34px");
		allnode.add(image_19);
		for(Image i:allnode){
			i.setVisible(false);
		}
		int i=0;
		for(Node node:rack.nodes){
			final Node temp=node;
			allnode.get(i).setVisible(true);
			allnode.get(i).setAltText(node.nodeName);
			allnode.get(i).setTitle(node.nodeName);
			if(!node.status){
				allnode.get(i).setUrl("images/NodeDead.png");
			}
			allnode.get(i).addClickHandler(new ClickHandler() {
				
				@Override
				public void onClick(ClickEvent event) {
					NodeServiceAsync async=GWT.create(NodeService.class);
					async.getDetails(temp.nodeName, new AsyncCallback<NodeDetails>() {
						
						@Override
						public void onSuccess(NodeDetails result) {
							NodeViewer viewer=new NodeViewer(result);
							viewer.setAnimationEnabled(true);
							viewer.setGlassEnabled(true);
							viewer.setModal(true);
							viewer.center();
						}
						
						@Override
						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub
							
						}
					});
				}
			});
			i++;
		}
		
	}
}

